package cn.lhf.cd.controller;

import cn.lhf.cd.FXApplication;
import cn.lhf.cd.annotation.FXController;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.Stack;

/**
 * 主Controller，处理主界面的动作
 */
@FXController(path = "/ui/main.fxml")
public class MainController implements Initializable {
    public BorderPane frame;
    public Label frameTitle;
    public AnchorPane parent;

    private Integer selfId;
    private Stack<ChildrenController> controllerStack = new Stack<>();
    private Stack<Pane> frameStack = new Stack<>();

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        try {
            setFrameView(LoginController.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置小标题
     * @param title 标题文本
     */
    public void setFrameTitleText(String title) {
        frameTitle.setText(title);
    }

    /**
     * 显示子视图
     *
     * @param clz   子控制器的Class
     * @param args  可选参数
     * @param <T>   返回值泛型
     * @return      子控制器实例
     * @throws Exception 神奇的异常
     */
    public <T> T setFrameView(Class<T> clz, Object ... args) throws Exception {
        FXMLLoader loader = FXApplication.loadFXML(clz);    //加载子视图的fxml

        Pane pane = loader.load();  //加载面板
        frame.setCenter(pane);      //显示面板
        frameStack.push(pane);      //保存面板，迷惑行为之一
        ChildrenController controller = loader.getController(); //得到子控制器实例
        setFrameTitleText(controller.getTitle());               //设小标题
        controllerStack.push(controller);                       //保存子控制器
        controller.bind(this, args);                //绑定主控制器，并传入可选参数
        return loader.getController();
    }

    /**
     * 左1按钮
     *
     * @param event 事件
     */
    public void onBtnLeft1(ActionEvent event) throws Exception {
        controllerStack.peek().onBtnLeft1();
    }

    /**
     * 左2按钮
     *
     * @param event 事件
     */
    public void onBtnLeft2(ActionEvent event) throws Exception {
        controllerStack.peek().onBtnLeft2();
    }

    /**
     * 左3按钮
     *
     * @param event 事件
     */
    public void onBtnLeft3(ActionEvent event) throws Exception {
        controllerStack.peek().onBtnLeft3();
    }

    /**
     * 左4按钮
     *
     * @param event 事件
     */
    public void onBtnLeft4(ActionEvent event) throws Exception {
        controllerStack.peek().onBtnLeft4();
    }

    /**
     * 右1按钮
     *
     * @param event 事件
     */
    public void onBtnRight1(ActionEvent event) throws Exception {
        controllerStack.peek().onBtnRight1();
    }

    /**
     * 右2按钮
     *
     * @param event 事件
     */
    public void onBtnRight2(ActionEvent event) throws Exception {
        controllerStack.peek().onBtnRight2();
    }

    /**
     * 右3按钮
     *
     * @param event 事件
     */
    public void onBtnRight3(ActionEvent event) throws Exception {
        controllerStack.peek().onBtnRight3();
    }

    /**
     * 右4按钮
     *
     * @param event 事件
     */
    public void onBtnRight4(ActionEvent event) throws Exception {
        controllerStack.peek().onBtnRight4();
    }

    /**
     * 返回首页
     */
    public void goHome() {
        while (controllerStack.size() > 2) {
            frameStack.pop();
            controllerStack.pop();
        }
        frame.setCenter(frameStack.peek());
    }

    public Integer getSelfId() {
        return selfId;
    }

    public void setSelfId(Integer selfId) {
        this.selfId = selfId;
    }
}
